{% extends "base.html" %}
{% block content %}
<h2 class="mb-4">📊 Reports</h2>

<!-- -------------------
     Filter: Available staff by date
-------------------- -->
<section class="card p-3 mb-4">
  <h4 class="mb-2">Available Staff Filter</h4>
  <form method="get" action="{{ url_for('reports') }}" class="d-flex align-items-center gap-2">
    <label for="date" class="form-label mb-0">Select Date:</label>
    <input
      type="date"
      id="date"
      name="date"
      value="{{ date }}"
      class="form-control"
      style="max-width: 200px;"
      required
    />
    <button type="submit" class="btn btn-primary">Load</button>
  </form>
</section>

<!-- -------------------
     Appointments Details
-------------------- -->
<section class="card p-3 mb-4">
  <h4 class="mb-3">Appointments (Detailed)</h4>
  {% if appointments and appointments|length > 0 %}
  <div class="table-responsive">
    <table class="table table-striped">
      <thead class="table-light">
        <tr>
          <th>Appointment ID</th>
          <th>Datetime</th>
          <th>Patient</th>
          <th>Staff</th>
          <th>Department</th>
        </tr>
      </thead>
      <tbody>
        {% for a in appointments %}
        <tr>
          <td>{{ a.appointment_id }}</td>
          <td>{{ a.appointment_datetime }}</td>
          <td>{{ a.patient_first_name }} {{ a.patient_last_name }}</td>
          <td>{{ a.staff_first_name }} {{ a.staff_last_name }}</td>
          <td>{{ a.department_name or "-" }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
    <p class="text-muted">No appointment data available.</p>
  {% endif %}
</section>

<!-- -------------------
     Revenue by Department
-------------------- -->
<section class="card p-3 mb-4">
  <h4 class="mb-3">Revenue per Department</h4>
  {% if revenue and revenue|length > 0 %}
  <div class="table-responsive">
    <table class="table table-striped">
      <thead class="table-light">
        <tr><th>Department</th><th>Revenue</th></tr>
      </thead>
      <tbody>
        {% for r in revenue %}
        <tr><td>{{ r.name }}</td><td>{{ r.revenue }}</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
    <p class="text-muted">No revenue data available.</p>
  {% endif %}
</section>

<!-- -------------------
     Outstanding Patients
-------------------- -->
<section class="card p-3 mb-4">
  <h4 class="mb-3">Patients with Outstanding Invoices</h4>
  {% if outstanding and outstanding|length > 0 %}
  <div class="table-responsive">
    <table class="table table-striped">
      <thead class="table-light">
        <tr><th>Patient</th><th>Invoice</th><th>Balance</th></tr>
      </thead>
      <tbody>
        {% for o in outstanding %}
        <tr>
          <td>{{ o.first_name }} {{ o.last_name }}</td>
          <td>{{ o.invoice_id }}</td>
          <td>{{ o.balance }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
    <p class="text-muted">No outstanding invoices.</p>
  {% endif %}
</section>

<!-- -------------------
     Available Staff (filtered)
-------------------- -->
{% if date %}
<section class="card p-3 mb-4">
  <h4 class="mb-3">Available Staff on {{ date }}</h4>
  {% if available and available|length > 0 %}
  <div class="table-responsive">
    <table class="table table-striped">
      <thead class="table-light">
        <tr><th>Staff ID</th><th>Name</th><th>Specialization</th></tr>
      </thead>
      <tbody>
        {% for s in available %}
        <tr>
          <td>{{ s.staff_id }}</td>
          <td>{{ s.first_name }} {{ s.last_name }}</td>
          <td>{{ s.specialization or "N/A" }}</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
    <p class="text-muted">No available staff found for {{ date }}.</p>
  {% endif %}
</section>
{% endif %}
{% endblock %}
